package pl.edu.agh.ki.ewywiadowka.client.windows;

import pl.edu.agh.ki.ewywiadowka.client.Const;
import pl.edu.agh.ki.ewywiadowka.client.EWywiadowka;
import pl.edu.agh.ki.ewywiadowka.client.dto.AttendantDTO;
import pl.edu.agh.ki.ewywiadowka.client.dto.ClassDTO;
import pl.edu.agh.ki.ewywiadowka.client.dto.SystemUserDTO;
import pl.edu.agh.ki.ewywiadowka.client.dto.TeacherDTO;
import pl.edu.agh.ki.ewywiadowka.client.dto.UserDTO;
import pl.edu.agh.ki.ewywiadowka.client.windows.attendant.AttendantWelcomeWindow;
import pl.edu.agh.ki.ewywiadowka.client.windows.educator.EducatorWelcomeWindow;
import pl.edu.agh.ki.ewywiadowka.client.windows.secretary.SecretaryWelcomeWindow;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class LoginWindow extends GenericWindow {
	
	private VerticalPanel _mainPanel = new VerticalPanel();
	private Grid _formular;
	private TextBox _loginTextBox = new TextBox();
	private TextBox _passwordTextBox = new TextBox();
	private Button _loginButton = new Button("Login");
	
	private class LoginWindowEventHandler implements ClickHandler, KeyUpHandler {
		
		public void verifyUser() {
			String login = _loginTextBox.getText();
			String password = _passwordTextBox.getText();
			
			Const.eWywiadowkaService.getSystemUser(login, password, new AsyncCallback<SystemUserDTO>() {

				@Override
				public void onFailure(Throwable caught) {
					Window.alert(caught.getMessage());
				}

				@Override
				public void onSuccess(SystemUserDTO result) {
					
					UserDTO user = result.user;
					
					//ten case to tak naprawde moglby byc dla Unknown i reszta to default
					switch (user.attr) {
						case UNKNOWN:
							Window.alert("Zly login lub haslo");
							break;
						case EDUCATOR:
							final TeacherDTO educator = (TeacherDTO) result;
							Const.eWywiadowkaService.getEducatorsClass(educator, new AsyncCallback<ClassDTO>() {

								@Override
								public void onFailure(Throwable caught) {
									Window.alert(caught.getMessage());
								}

								@Override
								public void onSuccess(ClassDTO result) {
									if (result == null) {
										Window.alert("Nie jestes wychowawca zadnej klasy\nZglos blad w sekretariacie");
									} else {
										getWywiadowka()._clas = result;
										getWywiadowka()._educator = educator;
										getWywiadowka().setUser(educator);
										getWywiadowka().putWindow(new EducatorWelcomeWindow(getWywiadowka(), educator, result));
									}
								}
								
							});
							break;
						case ATTENDANT:
							final AttendantDTO attendant = (AttendantDTO) result;
							if (attendant.students.size() == 0) {
								Window.alert("Nie jestes opiekunem zadnego ucznia\nZglos blad w sekretariacie");
								break;
							}
							getWywiadowka()._attendant = attendant;
							getWywiadowka().setUser(result);
							getWywiadowka().putWindow(new AttendantWelcomeWindow(getWywiadowka(), attendant));
							break;
						case SECRETARY:
							getWywiadowka().setUser(result);
							getWywiadowka().putWindow(new SecretaryWelcomeWindow(getWywiadowka(), result));
							break;
						default:
							Window.alert("Zly login lub haslo");
							break;
					}
				}
				
			});
		}

		@Override
		public void onClick(ClickEvent event) {
			verifyUser();
		}

		@Override
		public void onKeyUp(KeyUpEvent event) {
			if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
				verifyUser();
			}
		}
		
	}
	
	public LoginWindow(EWywiadowka wywiadowka) {
		
		super(wywiadowka);
		
		_formular = new Grid(2,2);
		_formular.setWidget(0, 0, new Label("login: "));
		_formular.setWidget(0, 1, _loginTextBox);
		_formular.setWidget(1, 0, new Label("password: "));
		_formular.setWidget(1, 1, _passwordTextBox);
		
		_loginTextBox.setText("kazia");
		_passwordTextBox.setText("123");
		
		LoginWindowEventHandler handler = new LoginWindowEventHandler();
		_loginTextBox.addKeyUpHandler(handler);
		_passwordTextBox.addKeyUpHandler(handler);
		_loginButton.addClickHandler(handler);

		_mainPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		_mainPanel.add(_formular);
		_mainPanel.add(_loginButton);
		
		this.initWidget(_mainPanel);
	}
	
}
